package com.hnev.gui;

import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;

public class AddFruit extends JFrame {
	JPanel panel;//中间容器
	JLabel lblId,lblName,lblPrice,lblAmount;
	JTextField txtId,txtName,txtPrice,txtAmount;
	JButton btOk,btReturn;
	
	public AddFruit(){
		setTitle("水果超市信息添加界面");
		setLocation(400, 80);//位置
		panel = new JPanel(new GridLayout(5,1));
		panel.setBorder(new EmptyBorder(100,5,100,5));//上左下右
		lblId=new JLabel("编      号：");
		lblName=new JLabel("姓        名：");
		lblPrice=new JLabel("价          格：");
		lblAmount=new JLabel("数           量：");
		txtId=new JTextField(16);
		txtName=new JTextField(16);
		txtPrice=new JTextField(16);
		txtAmount=new JTextField(16);
		btOk = new JButton("确认");
		btReturn = new JButton("返回");
		
		
		
		JPanel p1= new JPanel(new FlowLayout(FlowLayout.CENTER));
		p1.add(lblId);
		p1.add(txtId);
		panel.add(p1);
		
		JPanel p2= new JPanel(new FlowLayout(FlowLayout.CENTER));
		p2.add(lblName);
		p2.add(txtName);
		panel.add(p2);
		
		JPanel p3= new JPanel(new FlowLayout(FlowLayout.CENTER));
		p3.add(lblPrice);
		p3.add(txtPrice);
		panel.add(p3);
		
		
		JPanel p4= new JPanel(new FlowLayout(FlowLayout.CENTER));
		p4.add(lblAmount);
		p4.add(txtAmount);
		panel.add(p4);
		
		JPanel p5= new JPanel(new FlowLayout(FlowLayout.CENTER,100,0));
		p5.add(btOk);
		p5.add(btReturn);
		panel.add(p5);
		
		this.add(panel);
		this.setSize(600, 600);
		this.setLocation(400, 80);
		this.setLocationRelativeTo(null);
		this.setResizable(false);
		this.setVisible(true);
		
		class MyActionListener implements ActionListener{

			@Override
			public void actionPerformed(ActionEvent e) {
				
				Object source=e.getSource();
				if(source.equals(btOk)){
					try{
						// 获得连接
						Class.forName("com.mysql.jdbc.Driver");
						String url = "jdbc:mysql://localhost:3306/fruitSystem";
						String user ="root";
						String password="998520";
						Connection conn= DriverManager.getConnection(url,user,password);
						// 建立查询条件
						int id =Integer.parseInt(txtId.getText());
						String name =txtName.getText().trim();
						double price =Double.parseDouble(txtPrice.getText());
						double amount =Double.parseDouble(txtAmount.getText());
						//(10,"哈密瓜","8.98","15.0");
						String sql = "INSERT INTO fruit VALUES("+id+",\""+name+"\",+\""+price+"\",+\""+amount+"\");";  
						Statement sta = conn.createStatement();
		                int result = sta.executeUpdate(sql);
		                if (result > 0) {
		                	JOptionPane.showMessageDialog(null, "数据添加成功", "提示",JOptionPane.PLAIN_MESSAGE); 
		                  } 
		                new JDBC();
		                
	                	
						}catch(ClassNotFoundException cnfe){
						JOptionPane.showMessageDialog(null,"数据源错误","错误提示框",JOptionPane.ERROR_MESSAGE);
						}catch(SQLException sqle){
							sqle.printStackTrace();
						JOptionPane.showMessageDialog(null,"数据操作错误","错误提示框",JOptionPane.ERROR_MESSAGE);
						}
						
					
					

					
				
				}else if(source.equals(btReturn)){
					new JDBC();
				}
			
			}
		}

		MyActionListener listener = new MyActionListener();
		btOk.addActionListener(listener);
		btOk.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				
				
			}
		});
	  }



	
public static void main(String[] args) {
	new AddFruit();
}
}

